﻿@using BootstrapBlazor.Components
@using Microsoft.AspNetCore.SignalR.Client
@using TrackSystem.IO.Formula
@using TrackSystem.IO.Models

<Card Color="Color.Primary">
    <HeaderTemplate>
        <div class="col-6 col-sm-4 col-md-6 col-lg-6">
            <p class="card-text">通信端口  @chn.Name</p>
        </div>

        <div class="col-6 col-sm-4 col-md-6 col-lg-6">
            <Switch OnColor="Color.Success" OnText="通信使能" OffText="通信关闭"
                    @bind-value="@chn.Enabled"
                    OnValueChanged="@((a) => SetValue(chn.Name??="", a))" />
        </div>
    </HeaderTemplate>

    <BodyTemplate>
        <p class="card-text">端口描述: @chn.Description</p>
        <p class="card-text">端口属性: @chn.Attr</p>
        <p class="card-text">端口协议: @chn.Protocol</p>
        <div class="row g-3">
            <div class="col-6 col-sm-4 col-md-3 col-lg-auto">
                <Switch OnColor="Color.Info" IsDisabled="true" OnText="运行" OffText="停止"
                @bind-value="@chn.Running"/>
            </div>
            <div class="col-6 col-sm-4 col-md-3 col-lg-auto">
                <Switch OnColor="Color.Info" IsDisabled="true" OnText="端口连接" OffText="端口断开" @bind-value="@chn.Connected" />
            </div>
        </div>
        @{
            var d = chn.Cycle == 0? 0 : chn.Lapse * 100 / chn.Cycle;
        }

        <Divider />
        <div class="row g-3">
            <div class="col-6 col-sm-4 col-md-3 col-lg-auto">
                <p>周期: @chn.Interval ms</p>
            </div>
            <div class="col-6 col-sm-4 col-md-3 col-lg-auto">
                <p>实际: @chn.Cycle ms</p>
            </div>
            <div class="col-6 col-sm-4 col-md-3 col-lg-auto">
                <p>通信: @chn.Lapse ms</p>
            </div>
        </div>
        <BootstrapBlazor.Components.Progress Value="@d" IsShowValue="true"></BootstrapBlazor.Components.Progress>
        @if (chn.SensorNameList is not null && chn.SensorNameList.Any())
        {
            <Divider />
            <div class="row g-3">
                @foreach (var sensor in chn.SensorNameList)
                {
                    <div class="col-6 col-sm-4 col-md-3 col-lg-auto">
                        <Button class="apple-btn" Color="@(sensor == name ? Color.Success : Color.Dark)"
                                onclick="@(() => SelectedSensor.InvokeAsync(sensor))">
                            @sensor
                        </Button>

                    </div>
                }
            </div>
        }

    </BodyTemplate>

    <FooterTemplate>
        <Row ItemsPerRow="ItemsPerRow.Three">
            @if (chn.Type == Entities.Extensions.PortCategory.TCP)
            {
                <img src="images/TCP.png" height="30" width="50" />
            }
            else if (chn.Type == Entities.Extensions.PortCategory.UDP)
            {
                <img src="images/UDP.png" height="30" width="50" />
            }
            else if (chn.Type == Entities.Extensions.PortCategory.SRIAL)
            {
                <img src="images/SERIAL.png" height="30" width="50" />
            }
        </Row>
    </FooterTemplate>

</Card>

@code {
    [Parameter]
    public IOUnit chn { get; set; }

    [Parameter]
    public HubConnection hub { get; set; }

    [Parameter]
    public string? name { get; set; }

    [Parameter]
    public EventCallback<string> SelectedSensor { get; set; }

    /// <summary>
    /// 修改值
    /// </summary>
    /// <param name="name"></param>
    /// <param name="flag"></param>
    /// <returns></returns>
    private async Task SetValue(string name, bool flag)
    {
        try
        {
            if (hub.State == HubConnectionState.Connected)
                await hub.SendAsync("Assign", $"{name}_Enable", new OperandDef(flag));
        }
        catch (Exception _)
        {
            // ignored
        }
    }
}
